<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class GoodsController extends Controller
{
    //
    public function getAdd()
    {
    	$data = DB::table('cate')
    		-> select('*',DB::raw("CONCAT(path,',',id) AS sortPath"))
    		-> orderBy('sortPath')
    		-> get();
    	foreach ($data as $k => $v) {
    		$num = substr_count($v -> path, ',');
    		$data[$k] -> name = str_repeat('|----',$num).$v -> name;
    	}
    	return view('admin.goods.add',['data' => $data]);
    }
    public function postInsert(Request $request)
    {
    	// dd($request -> all());
    	$data = $request -> except('_token');
    	// dd($data);
    	// 上传图片
    	if($request -> hasFile('img'))
    	{
    		if($request -> file('img') -> isValid())
    		{
    			$suffix = $request -> file('img') -> getClientOriginalExtension();
    			$filename = time().mt_rand(100000,999999);
    			$res = $request -> file('img') -> move('./uploads/',$filename.'.'.$suffix);
    			if($res)
    			{
    				$data['img'] = $filename.'.'.$suffix;
    				$data['status'] = 0;
				}
    		}
    	}
    	$r = DB::table('goods') -> insert($data);
		if($r)
		{
			return redirect('/admin/goods/index') -> with(['info' => '添加成功']);
		}else
		{
			return back() -> with(['info' => '添加失败']);
		}
    			
    }
    public function getIndex(Request $request)
    {
    	$data = DB::table('goods')
    				-> leftJoin('cate','goods.cate_id','=','cate.id')
    				-> select('goods.*','cate.name AS cateName')
    				-> where('goods.name','like',"%".$request -> input('search')."%")
    				-> paginate($request -> input('num',10));
    	// dd($data);

    	return view('admin.goods.index',['data' => $data,'request' => $request -> all()]);
    }
    public function getEdit($id)
    {
    	$data = DB::table('goods') -> where('id',$id) -> first();

    	$allData = DB::table('cate')
    		-> select('*',DB::raw("CONCAT(path,',',id) AS sortPath"))
    		-> orderBy('sortPath')
    		-> get();
    	foreach ($allData as $k => $v) {
    		$num = substr_count($v -> path, ',');
    		$allData[$k] -> name = str_repeat('|----',$num).$v -> name;
    	}
    	// dd($data);
    	return view('admin.goods.edit',['data' => $data,'alldata' => $allData]);
    }
    public function postUpdate(Request $request)
    {
    	// dd($request -> all());
    	
    	$data = $request -> except('_token');
    	

    	if($request -> hasFile('img'))
    	{
    		if($request -> file('img') -> isValid())
    		{
    			$oldfilename = DB::table('goods') -> where('id',$data['id']) -> first() -> img;
    			// dd($oldfilename);
    			$suffix = $request -> file('img') -> getClientOriginalExtension();
    			$filename = time().mt_rand(100000,999999);
    			$request -> file('img') -> move('./Uploads',$filename.'.'.$suffix);
    			$data['img'] = $filename.'.'.$suffix;
    			if(file_exists('./uploads/'.$oldfilename))
    			{
    				unlink('./uploads/'.$oldfilename);
    			}
    		}
    	}

    	$res = DB::table('goods') ->where('id',$data['id'])  -> update($data);
    	if($res)
		{
			return redirect('/admin/goods/index') -> with(['info' => '更新成功']);
		}else
		{
			return back() -> with(['info' => '更新失败']);
		}
    }
    public function getDelete($id)
    {
    	$oldfilename = DB::table('goods') -> where('id',$id) -> first() -> img;
    	if(file_exists('./uploads/'.$oldfilename))
		{
			unlink('./uploads/'.$oldfilename);
		}
    	$res = DB::table('goods') -> delete($id);
    	if($res)
		{
			return redirect('/admin/goods/index') -> with(['info' => '删除成功']);
		}else
		{
			return back() -> with(['info' => '删除失败']);
		}
    }





}
